#============================================================
#	节点路径移动
#============================================================
# * 调用 set_custom_move_method 方法设置自定义移动方式
# * 默认移动方式为下面的 _default_method 方法，默认以 Control 类型
#============================================================
# @datetime: 2022-5-5 21:19:33
#============================================================
tool
class_name PathMove
extends Node


## 按照这个 Path2D 移动
export var path : NodePath setget set_path 


var _path_player : _PathPlayer = _PathPlayer.new()


#============================================================
#   Set/Get
#============================================================
func set_path(value: NodePath) -> void:
	path = value
	update_configuration_warning()


##  设置自定义移动的方法
## （这个方法必须有两个参数，第一个接收 Control 类型的节点，第二个为路径上的位置 Vector2D 类型）
## @object  
## @method  
func set_custom_move_method(object: Object, method: String):
	_path_player.set_custom_move_method(object, method)


#============================================================
#   内置
#============================================================
func _enter_tree():
	_path_player._path = get_node_or_null(path) as Path2D

func _ready():
	if Engine.editor_hint:
		set_process(false)
	set_custom_move_method(self, "_default_method")

func _process(delta):
	_path_player._process(delta)


func _get_configuration_warning():
	if path == "":
		return "没有设置 path 属性"
	var p = get_node_or_null(path)
	if not p is Path2D:
		return "选中的节点必须是 Path2D 类型的节点"
	return ""


#============================================================
#   自定义
#============================================================
## 默认移动方式
func _default_method(host, pos: Vector2):
	if host is Node2D:
		host.global_position = pos
	elif host is Control:
		host.rect_global_position = pos


##  播放节点移动
## @object  
## @time  
func play(object: Control, time: float):
	_path_player.add_task(object, time)



#============================================================
#   路径动画播放器
#============================================================
class _PathPlayer:
	
	var _tasks = {}
	var _path : Path2D
	var _func : FuncRef
	
	func set_custom_move_method(object: Object, method: String):
		_func = funcref(object, method)
	
	func _process(delta: float):
		for task in _tasks.keys():
			if task.execute(delta):
				_tasks.erase(task)
	
	func add_task(object: Node, time: float):
		var task = Task.new(_path, time)
		task._host = object
		task._curve = _path.curve
		task._func = _func
		_tasks[task] = null
	
	class Task:
		var _host : Node
		var _curve : Curve2D
		var _func : FuncRef
		
		var _length_step : float
		var _length_max : float
		var _length : float
		
		func _init(path: Path2D, time_max: float):
			_length_max = path.curve.get_baked_length()
			_length_step = _length_max / time_max
		
		func execute(delta: float):
			_length += _length_step * delta
			var pos : Vector2 = _curve.interpolate_baked(_length)
			_func.call_func(_host, pos)
			if _length >= _length_max:
				return true



